package com.commerceerp.system.util;

import com.commerceerp.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;


/**
 *
 */
@Configuration
@EnableScheduling // 启用定时任务
public class SchedulingConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WishService wishService;

    @Autowired
    private AmazonService amazonService;

    @Autowired
    private EbayService ebayService;

    @Autowired
    private ImageService imageService;

    @Autowired
    private WinitService winitService;

    @Scheduled(cron = "0 0 */4 * * ?") // 每2小时执行一次
    public void synOrder() {
        logger.info(">>>>>>>>>>>>> 启动wish订单同步↓");
        try{
            wishService.getWishOrder(null);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info(">>>>>>>>>>>>> 启动wish订单同步↑");

        logger.info(">>>>>>>>>>>>> 启动amazon订单同步↓");
        try{
            String date = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),-7);
            amazonService.getOrder(null,date);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info(">>>>>>>>>>>>> 启动amazon订单同步↑");

        logger.info(">>>>>>>>>>>>> 启动ebay订单同步↓");
        try{
            String date = DateUtil.getYMDHMSDate();
            date = DateUtil.getAfterDate(date,1);
            String date2 = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),-7);
            ebayService.getEbayOrders(null,null,date,date2);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info(">>>>>>>>>>>>> 启动ebay订单同步↑");
    }

    @Scheduled(cron = "0 30 2 * * ?") // 每2:30分执行一次
    public void synMessage() {
        logger.info(">>>>>>>>>>>>> 启动wish消息同步↓");
        try{
            wishService.wishTicket(null);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info(">>>>>>>>>>>>> 启动wish消息同步↑");

        logger.info(">>>>>>>>>>>>> 启动ebay消息同步↓");
        try{
            String date = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),-3);
            String date2 = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),1);
            ebayService.getMessage(null,null,date,date2);
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info(">>>>>>>>>>>>> 启动ebay消息同步↑");
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void synHuilv(){
        ebayService.huilv();
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void imageSpace(){
        imageService.imageSpace();
    }

    @Scheduled(cron = "0 41 13 * * ?")
    public void sku(){
        amazonService.getImgUrl();
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void getWarehouse(){
        winitService.addWarehouse();
    }

}
